// pages/cart/cart.js
const user = getApp().globalData.userInfo
const _that = this

Page({

    /**
     * 页面的初始数据
     */
    data: {
        carList:[],
        totalPrice:0,
        selectAllStatus: false,
        Cars:[]
    },
    //加
    plus:function (e) {
        var ids = e.currentTarget.dataset.id
        const that = this
        wx.request({
            url: 'http://localhost:8888/plusNum', 
           data:{
            car_id:ids
           },//仅为示例，并非真实的接口地址
           success () {
                that.getTotalPrice()
                that.getAllGood()
           }
         })  
    },
    //删除
    del:function (e) {
        var ids = e.currentTarget.dataset.id
        const that = this
        wx.request({
            url: 'http://localhost:8888/deleteCar', 
           data:{
            car_id:ids,
           },//仅为示例，并非真实的接口地址
           success () {
                that.getAllGood()
           }
         })  
    },
    //全选
    selectAll:function(e) {
        const that = this
        const selectAllStatus = !this.data.selectAllStatus;
        let car = this.data.carList;
        for(let i = 0;i<car.length;i++){
          
        }
        this.setData({
            carList:car,
            selectAllStatus:selectAllStatus
        })
        wx.request({
          url: 'http://localhost:8888/updateAllCarState', 
          data:{
              selectAllStatus,
              wx_userid:user[3]
          },
          success () {
            that.getAllGood()
       }
       })
        this.getTotalPrice();
      },
    //单选
      selectThis:function(e) {
        console.log(e);
        const index = e.currentTarget.dataset.index;
        const that = this
        let car = this.data.carList;
        car[index].state = !car[index].state;
            this.setData({
                carList: car,
                selectAllStatus:false,
            });
        that.getTotalPrice();
        const ids = e.currentTarget.dataset.id;
        const states = e.currentTarget.dataset.state;
        const user = e.currentTarget.dataset.user;
        wx.request({
          url: 'http://localhost:8888/updateCarState', 
          data:{
              car_id:ids,
              state:states,
          },
          success () {
            that.getAllGood()
       }
       })
      },
      //减
      reduce:function (e) {
        const index = e.currentTarget.dataset.id;
        let car = this.data.carList;
        for(let i = 0 ;i < car.length;i++){
            console.log(i);
            if(car[i].car_id == index){
                if(car[i].goodNum <= 1){
                    wx.showToast({
                        title: '最少为1',
                        icon: 'none',
                        duration: 2000
                      })
                }else{
                        var ids = e.currentTarget.dataset.id
                        const that = this
                        wx.request({
                            url: 'http://localhost:8888/reduceNum', 
                           data:{
                            car_id:ids
                           },//仅为示例，并非真实的接口地址
                           success () {
                                that.getAllGood()
                           }
                         })  
                }
            }
        }
      },
    /**
     * 生命周期函数--监听页面加载
     */
          //合计
          getTotalPrice() {
            let car = this.data.carList;                  // 获取购物车列表
            let total = 0;
            for (let i = 0; i < car.length; i++) {         // 循环列表得到每个数据
              if (car[i].state) {                     // 判断选中才会计算价格
                total += car[i].goodNum * car[i].goodPrice;   // 所有价格加起来
              }
            }
            this.setData({                                // 最后赋值到data中渲染到页面
              carList: car,
              totalPrice: total.toFixed(2)
            });
          },
    getAllGood:function() {
        console.log(user)
        const that = this
        wx.request({
            url: 'http://localhost:8888/getAllCar', 
            data:{
               wx_userid: user[3]
            },
           success (res) {
            var carAll = res.data.data
            console.log(res)
            that.setData({
            carList:carAll
          })
          that.getTotalPrice()
           }
         })
    },
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.getAllGood()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})